package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.Record;
import com.vehicle.bean.Violation;
import com.vehicle.feign.AccountFeign;
import com.vehicle.feign.ParkingFeign;
import com.vehicle.form.AddRecordForm;
import com.vehicle.mapper.RecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.mapper.UserLogMapper;
import com.vehicle.service.RecordService;
import com.vehicle.util.LogUtil;
import com.vehicle.util.TimeUtilFormat;
import com.vehicle.vo.AdminRecordVO;
import com.vehicle.vo.RecordInfoVO;
import com.vehicle.vo.UserRecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-21
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {

    @Autowired
    RecordMapper recordMapper;

    @Autowired
    AccountFeign accountFeign;

    @Autowired
    ParkingFeign parkingFeign;

    @Autowired
    UserLogMapper userLogMapper;

    @Override
    public AdminRecordVO getAllRecordList(Integer page, Integer size) {
        Page<Record> infoPage = new Page<>(page,size);
        Page<Record> recordInfoPage = recordMapper.selectPage(infoPage,null);
        AdminRecordVO adminRecordVO = new AdminRecordVO();
        adminRecordVO.setSize(recordInfoPage.getSize());
        adminRecordVO.setTotal(recordInfoPage.getTotal());
        List<RecordInfoVO> recordInfoVOS = new ArrayList<>();
        List<Record> records = recordInfoPage.getRecords();
        for (Record r:records) {
            RecordInfoVO recordInfoVO = new RecordInfoVO();
            BeanUtils.copyProperties(r,recordInfoVO);
            recordInfoVO.setLocation(parkingFeign.getLocationByParkingId(r.getParkingId()));
            recordInfoVO.setTel(accountFeign.getUserTel(r.getUserId()));
            recordInfoVO.setUserName(accountFeign.getUserName(r.getUserId()));
            if(r.getRecordState().equals("停车")){
                String ss = TimeUtilFormat.getTimeFormat(r.getCreateTime(), LocalDateTime.now());
                recordInfoVO.setTimeLong(ss);
            }else{
                String ss = TimeUtilFormat.getTimeFormat(r.getCreateTime(), r.getLeftTime());
                recordInfoVO.setTimeLong(ss);
            }
            recordInfoVOS.add(recordInfoVO);
        }
        adminRecordVO.setContent(recordInfoVOS);
        return adminRecordVO;
    }

    @Override
    public List<Record> getUserRecord(Integer id) {
        QueryWrapper<Record> recordQueryWrapper  = new QueryWrapper<>();
        recordQueryWrapper.eq("user_id",id);
        List<Record> records = recordMapper.selectList(recordQueryWrapper);
        System.out.println("records为");
        System.out.println(records);
//        for (Record r:records) {
//            List<String> pictureList = Arrays.asList(r.getPictureList().split(","));
//            r.setPictureList(pictureList.get(0));
//        }
        return records;
    }

    @Override
    public UserRecordVO getUserDetail(Integer id) {
            Record r = recordMapper.selectById(id);
            UserRecordVO recordInfoVO = new UserRecordVO();
            BeanUtils.copyProperties(r,recordInfoVO);
            recordInfoVO.setLocation(parkingFeign.getLocationByParkingId(r.getParkingId()));
            if(r.getRecordState().equals("停车中")){
                String ss = TimeUtilFormat.getTimeFormat(r.getCreateTime(),  LocalDateTime.now());
                System.out.println("得不到时间吗");
                recordInfoVO.setTimeLong(ss);
            }else{
                String ss = TimeUtilFormat.getTimeFormat(r.getCreateTime(),r.getLeftTime());
                recordInfoVO.setTimeLong(ss);
            }
            if(r.getPictureList()!=null){
                List<String> pictureList = Arrays.asList(r.getPictureList().split(","));
                recordInfoVO.setPictureList(pictureList);
            }else {
                recordInfoVO.setPictureList(null);
            }

        return recordInfoVO;
    }

    @Override
    public Record addRecordListIn(String ip,Integer id,AddRecordForm addRecordForm) {
        Record record = new Record();
        record.setUserId(addRecordForm.getUserId());
        record.setParkingId(addRecordForm.getParkingId());
        record.setRecordState("停车中");
        //将图片的list类型转为string类型
        if(addRecordForm.getPictureList()!=null) {
            String list = addRecordForm.getPictureList().toString();
            record.setPictureList(list);
        }
        int b = recordMapper.insert(record);
        if(b==1){
            //修改车位状态
            userLogMapper.insert(LogUtil.insertUserUpdate(id,"停车成功",ip,"成功"));
            parkingFeign.updateParkingState(addRecordForm.getParkingId(), 1);
            return record;
        }
        userLogMapper.insert(LogUtil.insertUserUpdate(id,"停车失败",ip,"失败"));
        return null;
    }

    @Override
    public Boolean addRecordListOut(String ip,Integer aid,Integer id) {
        UpdateWrapper<Record> recordUpdateWrapper = new UpdateWrapper<>();
        recordUpdateWrapper.eq("record_id",id);
        recordUpdateWrapper.set("left_time",LocalDateTime.now());
        recordUpdateWrapper.set("record_state","已离开");
        int r = recordMapper.update(null,recordUpdateWrapper);
        if(r==1){
            //修改车位状态
            parkingFeign.updateParkingState(id, 1);
            userLogMapper.insert(LogUtil.insertUserUpdate(aid,"还车成功",ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertUserUpdate(aid,"还车失败",ip,"失败"));
        return false;
    }

    @Override
    public List<RecordInfoVO> getNewTen() {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        Page<Record> pageInfo = new Page<>(1,5);
        Page<Record> page = recordMapper.selectPage(pageInfo,queryWrapper);
        List<RecordInfoVO> recordInfoVOS = new ArrayList<>();
        List<Record> records = page.getRecords();
        for (Record r:records) {
            RecordInfoVO recordInfoVO = new RecordInfoVO();
            BeanUtils.copyProperties(r,recordInfoVO);
            recordInfoVO.setLocation(parkingFeign.getLocationByParkingId(r.getParkingId()));
            recordInfoVO.setUserName(accountFeign.getUserName(r.getUserId()));
            recordInfoVOS.add(recordInfoVO);
        }
        return recordInfoVOS;
    }

    @Override
    public Integer getNumber(Integer userId) {
        return recordMapper.getParkingNumber(userId);
    }

    @Override
    public LocalDateTime getTime(Integer userId) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByAsc("create_time");
        queryWrapper.last("limit 1");
        Record record = recordMapper.selectOne(queryWrapper);
        return record.getCreateTime();
    }

    @Override
    public Record existOrNot(Integer userId) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("record_state","停车中");
        Record r = recordMapper.selectOne(queryWrapper);
        return r;
    }

    @Override
    public Record existInKu(Integer parkingId) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parking_id",parkingId);
        queryWrapper.eq("record_state","停车中");
        Record r = recordMapper.selectOne(queryWrapper);
        return r;
    }

    @Override
    public boolean carExistInkU(Integer userId) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("record_state","停车中");
        List<Record> r = recordMapper.selectList(queryWrapper);
        if(r!=null){
            return true;
        }
        return false;
    }
}


